package com.yonyou.ucf.mdf.app.isvdxq.event;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyExtend;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import com.yonyou.ucf.mdf.app.isvdxq.yonyisv.model.EventContent;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.network.cryptor.EncryptionHolder;
import com.yonyoucloud.iuap.ucf.mdd.starter.ucg.openapi.module.isv.event.ISVEventListener;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.*;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Set;

/**
 * project name: idx02
 * Date: 2022/3/22 13:02
 *
 * @PackgeName: com.yonyou.ucf.mdf.app.isvdxq.event
 * @Author: GXG
 * @Version：1.00
 * @Description: 德鑫自定义订阅事件类
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ISVEventDemo implements ISVEventListener<EventContentDemo> {
    private static final Logger logger = LoggerFactory.getLogger(ISVEventDemo.class);
    @Autowired
    private BaseService baseService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ManageClassService manageClassService;
    @Autowired
    private ProductSKUService productSKUService;
    @Autowired
    private UnitService unitService;

    @Autowired
    private UnitassistService unitassistService;
    @Autowired
    private ProductSKUTempService productSKUTempService;
    @Autowired
    private ProductSpecService productSpecService;

    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private TranstypeService transtypeService;
    @Autowired
    private OrgUnitService orgUnitService;

    private ObjectMapper mapper = new ObjectMapper();

    @SneakyThrows
    @Override
    public boolean onEvent(String myType, EventContentDemo eventContentDemo, EncryptionHolder holder) {
        logger.info(">>>#onEvent#ISVEvent接收到消息:{}", myType);
        String tenantId = eventContentDemo.getTenantId();
        String content = eventContentDemo.getContent();
        //已经解密过了
        if (StringUtils.isNotBlank(content)) {
            String logtxt = String.format("[event]tid:%s,type:%s,eventId:%s", tenantId, myType, eventContentDemo.getEventId());
            System.out.println(logtxt);
            EventContent event = MyTool.convertBean2Bean(eventContentDemo, EventContent.class);
            switch (myType) {
                //业务单元 BASE_ORG_EVENT_ADD_AFTER、BASE_ORG_EVENT_UPDATE_AFTER、BASE_ORG_EVENT_DELETE_AFTER BASE_ORG_EVENT_DISABLE_AFTER、BASE_ORG_EVENT_ENABLE_AFTER
                case "BASE_ORG_EVENT_DELETE_AFTER":
                    logger.info("事件类型: {}, 说明: 业务单元删除", myType);
                    int flagOrgUnitDel = orgUnitService.EventDel(event);
                    if (flagOrgUnitDel > 0) {
                        logger.info("业务单元删除订阅成功{}", flagOrgUnitDel);
                    }
                    break;
                case "BASE_ORG_EVENT_ADD_AFTER":
                case "BASE_ORG_EVENT_UPDATE_AFTER":
                    logger.info("事件类型: {}, 说明: 业务单元编辑", myType);
                    orgUnitService.AutoDataEdit(event);
                    logger.info("业务单元编辑订阅成功{}");
                    break;
                //交易类型
                case "transtype_stop":
                case "transtype_unstop":
                    logger.info("事件类型: {}, 说明: 交易类型停用启用", myType);
                    break;
                case "transtype_delete":
                    logger.info("事件类型: {}, 说明: 交易类型删除", myType);
                    int flagTranstypeDel = transtypeService.EventDel(event);
                    if (flagTranstypeDel > 0) {
                        logger.info("交易类型订阅成功{}", flagTranstypeDel);
                    }
                    break;
                case "transtype_add":
                case "transtype_update":
                    logger.info("事件类型: {}, 说明: 交易类型编辑", myType);
                    int flagTranstypeEdit = transtypeService.EventEdit(event);
                    if (flagTranstypeEdit > 0) {
                        logger.info("交易类型订阅成功{}", flagTranstypeEdit);
                    }
                    break;
                //仓库
                case "YXYBASEDOC_AA_WAREHOUSE_DELETE":
                    logger.info("事件类型: {}, 说明: 仓库删除", myType);
                    int flagWarehouseDel = warehouseService.EventDel(event);
                    if (flagWarehouseDel > 0) {
                        logger.info("仓库订阅成功{}", flagWarehouseDel);
                    }
                    break;
                case "YXYBASEDOC_AA_WAREHOUSE_INSERT":
                case "YXYBASEDOC_AA_WAREHOUSE_UPDATE":
                    logger.info("事件类型: {}, 说明: 仓库编辑", myType);
                    try {
                        warehouseService.AutoDataEdit(event);
                    } catch (Exception ex) {
                        logger.info("[仓库编辑]{}", ex.getMessage());
                    }
                    break;
                //计量单位
                case "YXYBASEDOC_PC_UNIT_DELETE":
                    logger.info("事件类型: {}, 说明: 计量单位删除", myType);
                    int flagUnitDel = unitService.EventDel(event);
                    if (flagUnitDel > 0) {
                        logger.info("计量单位订阅成功{}", flagUnitDel);
                    }
                    break;
                case "YXYBASEDOC_PC_UNIT_INSERT":
                case "YXYBASEDOC_PC_UNIT_UPDATE":
                    logger.info("事件类型: {}, 说明: 计量单位编辑", myType);
                    int flagUnitEdit = unitService.EventEdit(event);
                    if (flagUnitEdit > 0) {
                        logger.info("计量单位订阅成功{}", flagUnitEdit);
                    }
                    break;
                //物料SKU
                case "YXYBASEDOC_PC_PRODUCTSKUPRO_DELETE":
                    logger.info("事件类型: {}, 说明: 物料SKU删除", myType);
                    int flagProductSKUDel = productSKUService.EventDel(event);
                    if (flagProductSKUDel > 0) {
                        logger.info("物料档案SKU订阅成功{}", flagProductSKUDel);
                    }
                    break;
                case "YXYBASEDOC_PC_PRODUCTSKUPRO_INSERT":
                case "YXYBASEDOC_PC_PRODUCTSKUPRO_UPDATE":
                    logger.info("事件类型: {}, 说明: 物料SKU编辑", myType);
                    int flagProductSKUEdit = productSKUService.EventEdit(event);
                    if (flagProductSKUEdit > 0) {
                        logger.info("物料档案SKU订阅成功{}", flagProductSKUEdit);
                    }
                    break;
                //物料
                case "YXYBASEDOC_PC_PRODUCT_DELETE":
                    logger.info("事件类型: {}, 说明: 物料删除", myType);
                    int flagProductDel = productService.EventDel(event);
                    if (flagProductDel > 0) {
                        logger.info("物料档案订阅成功{}", flagProductDel);
                    }
                    break;
                case "YXYBASEDOC_PC_PRODUCT_UPDATE":
                case "YXYBASEDOC_PC_PRODUCT_INSERT":
                    logger.info("事件类型: {}, 说明: 物料编辑", myType);
                    try {
                        productService.AutoDataEdit(event);
                    } catch (Exception ex) {
                        logger.info("[物料编辑]{}", ex.getMessage());
                    }
                    break;
                //物料分类
                case "YXYBASEDOC_PC_MANAGEMENTCLASS_INSERT":
                case "YXYBASEDOC_PC_MANAGEMENTCLASS_UPDATE":
                    logger.info("事件类型: {}, 说明: 物料分类编辑", myType);
                    int flagMANAGEMENTCLASSEdit = manageClassService.EventEdit(event);
                    if (flagMANAGEMENTCLASSEdit > 0) {
                        logger.info("物料分类订阅成功{}", flagMANAGEMENTCLASSEdit);
                    }
                    break;
                case "YXYBASEDOC_PC_MANAGEMENTCLASS_DELETE":
                    logger.info("事件类型: {}, 说明: 物料分类删除", myType);
                    int flagMANAGEMENTCLASSDel = manageClassService.EventDel(event);
                    if (flagMANAGEMENTCLASSDel > 0) {
                        logger.info("物料分类订阅成功{}", flagMANAGEMENTCLASSDel);
                    }
                    break;
                //物料规格
                case "YXYBASEDOC_PC_SPEPRO_INSERT":
                case "YXYBASEDOC_PC_SPEPRO_UPDATE":
                    logger.info("事件类型: {}, 说明: 物料规格编辑", myType);
                    int flagSPEPROEdit = productSpecService.EventEdit(event);
                    if (flagSPEPROEdit > 0) {
                        logger.info("物料规格订阅成功{}", flagSPEPROEdit);
                    }
                    break;
                case "YXYBASEDOC_PC_SPEPRO_DELETE":
                    logger.info("事件类型: {}, 说明: 物料规格删除", myType);
                    int flagSPEPRODel = productSpecService.EventDel(event);
                    if (flagSPEPRODel > 0) {
                        logger.info("物料规格订阅成功{}", flagSPEPRODel);
                    }
                    break;
                //其它
                case "CHECK_URL":
                    logger.info("事件类型: {}, 说明: 检查事件推送回调地址", myType);
                    break;

                case "STAFF_ADD":
                    logger.info("事件类型: {}, 说明: 租户 {} 下员工增加, 员工变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getStaffId()));
                    break;
                case "STAFF_UPDATE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下员工更改, 员工变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getStaffId()));
                    break;
                case "STAFF_ENABLE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下员工启用, 员工变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getStaffId()));
                    break;
                case "STAFF_DISABLE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下员工停用, 员工变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getStaffId()));
                    break;
                case "STAFF_DELETE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下员工删除, 员工变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getStaffId()));
                    break;

                case "DEPT_ADD":
                    logger.info("事件类型: {}, 说明: 租户 {} 下部门创建, 部门变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getDeptId()));
                    break;
                case "DEPT_UPDATE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下部门修改, 部门变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getDeptId()));
                    break;
                case "DEPT_ENABLE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下部门修改, 部门变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getDeptId()));
                    break;
                case "DEPT_DISABLE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下部门修改, 部门变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getDeptId()));
                    break;
                case "DEPT_DELETE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下部门删除, 部门变更 id: {}", myType, event.getTenantId(), Arrays.toString(event.getDeptId()));
                    break;
                case "USER_ADD":
                    logger.info("事件类型: {}, 说明: 租户 {} 下用户增加，用户 id: {}", myType, event.getTenantId(), Arrays.toString(event.getUserId()));
                    break;
                case "USER_DELETE":
                    logger.info("事件类型: {}, 说明: 租户 {} 下用户移除，用户 id: {}", myType, event.getTenantId(), Arrays.toString(event.getUserId()));
                    break;
                case "APPROVAL_MEG":
                    logger.info("事件类型: {}, 说明: 租户 {} 下用户移除", myType, event.getTenantId());
                    break;
                case "FRONT_LOGOUT":
                    logger.info("事件类型: {}, 说明: 租户 {} 用户注销，信息: {}", myType, event.getTenantId(), event.getContent());
                    break;
                case "META_ALTER":
                    logger.info("事件类型: {}, 说明: 租户 {} 元数据修改，信息: {}", myType, event.getTenantId(), event.getContent());
                    break;
                case "PROJECT_ADD":
                    logger.info("事件类型: {}, 说明: 租户 {} 元数据修改，信息: {}", myType, event.getTenantId(), event.getContent());
                    break;
                case "UNKNOWN":
                default:
                    logger.info("未知事件");
                    break;
            }
            return true;
        }
        return false;
    }

    @Override
    public int priority() {
        return ISVEventListener.super.priority() - 100;
    }

    @Override
    public Set<String> supportTypes() {
        return Sets.newHashSet(ISVEventDemoType.IsvType());
    }

    @Override
    public Class<EventContentDemo> getEventClass() {
        return EventContentDemo.class;
    }
}
